$(function () {
    tocbot.init({
        // 在何处呈现目录
        tocSelector: '.js-toc',
        // 在哪里抓取标题来构建目录
        contentSelector: '.js-toc-content',
        // 在content元素中抓取哪些标题
        headingSelector: 'h1, h2, h3',
        // For headings inside relative or absolute positioned containers within content.
        hasInnerContainers: true,
    });
    var length = $("#catalogue li").length;
    if (length==0){
        $("#catalogue").append('<li>无</li>');
    }
});

function share() {
    var url = window.location.href;
    $.get(
        CONTEXT_PATH+"/share/createImage",
        {"htmlUrl":url},
        function (data) {
            data=$.parseJSON(data);
            if (data.code==0){
                $("#hintModal").modal("show");
                //2秒后隐藏提示框
                setTimeout(function () {
                    $("#hintModal").modal("hide");
                    //刷新页面
                    location.href=CONTEXT_PATH+"/share/image/"+data.shareUrl;
                }, 5000);
            }else {
                alert("服务繁忙，请稍后再试~");
            }
        }
    )
}

function like(btn,entityType,entityId,entityUserId) {
    $.post(
        CONTEXT_PATH+"/doLike",
        {"entityId":entityId,"entityType":entityType,"entityUserId":entityUserId},
        function (data) {
            data=$.parseJSON(data);
            if (data.code==0){
                $(btn).children("i").text(data.likeCount);
                $(btn).children("img").attr("src",data.likeStatus==0?CONTEXT_PATH+"/img/like.png":CONTEXT_PATH+"/img/like-black.png");
            }else {
                alert(data.msg);
            }
        }
    )
}
function stick(btn,postId) {
    var text = $(btn).text();
    if (confirm("你确定要将此篇文章"+text+"吗？")) {
        $.post(
            CONTEXT_PATH + "/discuss/stick/" + postId,
            function (data) {
                data = $.parseJSON(data);
                alert(data.msg);
                if (data.code == 0) {
                    if ($(btn).hasClass("btn-secondary")) {
                        $(btn).text("置顶").removeClass("btn-secondary").addClass("btn-danger");
                    } else {
                        $(btn).text("取消置顶").removeClass("btn-danger").addClass("btn-secondary");
                    }
                }
            }
        )
    }
}
function quintessence(btn,postId) {
    var text = $(btn).text();
    if (confirm("你确定要将此篇文章"+text+"吗？")) {
        $.post(
            CONTEXT_PATH + "/discuss/quintessence/" + postId,
            function (data) {
                data = $.parseJSON(data);
                alert(data.msg);
                if (data.code == 0) {
                    if ($(btn).hasClass("btn-secondary")) {
                        $(btn).text("加精").removeClass("btn-secondary").addClass("btn-danger");
                    } else {
                        $(btn).text("取消精华").removeClass("btn-danger").addClass("btn-secondary");
                    }
                }
            }
        )
    }
}

function deleteDiscuss(postId) {
    if (confirm("你确定要将此篇文章删除吗？")){
        $.post(
            CONTEXT_PATH+"/discuss/delete/"+postId,
            function (data) {
                data = $.parseJSON(data);
                alert(data.msg);
                if (data.code == 0){
                    setTimeout(function () {
                        location.href = CONTEXT_PATH+"/index";
                    },1000);
                }
            }
        )
    }
}
